/**
 ******************************************************************************
 * @file    timer.h
 * @author  hyseim software Team
 * @date    18-Aug-2023
 * @brief   This file provides all the headers of the timer functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef _timer_H_
#define _timer_H_
#include "chip_define.h"

#ifdef __cplusplus
extern "C" {
#endif

#define  T0CVL    0x000
#define  T0CVH    0x004
#define  T0SVL    0x008
#define  T0SVH    0x00c
#define  T0CTL    0x010
#define  T0IRQS   0x014
#define  T1CVL    0x020
#define  T1CVH    0x024
#define  T1SVL    0x028
#define  T1SVH    0x02c
#define  T1CTL    0x030
#define  T1IRQS   0x034
#define  T2CVL    0x040
#define  T2CVH    0x044
#define  T2SVL    0x048
#define  T2SVH    0x04c
#define  T2CTL    0x050
#define  T2IRQS   0x054
#define  T3CVL    0x060
#define  T3CVH    0x064
#define  T3SVL    0x068
#define  T3SVH    0x06c
#define  T3CTL    0x070
#define  T3IRQS   0x074
#define  T4CVL    0x080
#define  T4CVH    0x084
#define  T4SVL    0x088
#define  T4SVH    0x08c
#define  T4CTL    0x090
#define  T4IRQS   0x094
#define  T5CVL    0x0a0
#define  T5CVH    0x0a4
#define  T5SVL    0x0a8
#define  T5SVH    0x0ac
#define  T5CTL    0x0b0
#define  T5IRQS   0x0b4
#define  T6CVL    0x0c0
#define  T6CVH    0x0c4
#define  T6SVL    0x0c8
#define  T6SVH    0x0cc
#define  T6CTL    0x0d0
#define  T6IRQS   0x0d4
#define  T7CVL    0x100
#define  T7CVH    0x104
#define  T7SVL    0x108
#define  T7SVH    0x10c
#define  T7CTL    0x110
#define  T7IRQS   0x114
//-------------------------------------------------------
//function declaration
//-------------------------------------------------------

/*!
 *	\fn   void start_time(void)/
 *	\brief Set control register and current load value
 */
extern void start_time(void);
/*!
 *	\fn    void stop_time(void)/
 *	\brief Generate an interrupt and clear
 */
extern void stop_time(void);
/*!
 *	\fn    void stop_time(void)/
 *	\brief Read current counter value
 */
//extern unsigned int get_time(void);

/*** part for coremark timer ***/
#define TIMER_PERIOD 0xffffffff

#define  T0CVL    0x000
#define  T0CVH    0x004
#define  T0SVL    0x008
#define  T0SVH    0x00c
#define  T0CTL    0x010
#define  T0IRQS   0x014

typedef struct TIMER
{
    volatile unsigned int    CurrentValueLow;		/* The value to be loaded into Timer */
    volatile unsigned int    CurrentValueHigh;	/* Timer Current Value */
    volatile unsigned int    LoadCountLow;		/* Timer Control Register */
    volatile unsigned int    LoadCountHigh;		/* Timer Clears the interrupt status */
    volatile unsigned int    Control;		/* Timer Contains the interrupt status */
    volatile unsigned int    IRQS;   /* Timer Contains the interrupt status */
}Struct_TIMER,* PStruct_TIMER;

/*
 * Start timer.
 */
static int start_timer()
{
  PStruct_TIMER pTimer = (PStruct_TIMER)(TIMERX6_BASE);
  pTimer->LoadCountLow = TIMER_PERIOD;
  pTimer->LoadCountHigh = 0;
  pTimer->Control = 0x00000003;
  pTimer->Control = 0x00000001;
  return  pTimer->CurrentValueLow;
}


static int start_set_timer(int a)
{
  PStruct_TIMER pTimer = (PStruct_TIMER)(TIMERX6_BASE);
  pTimer->LoadCountLow = a;
  pTimer->LoadCountHigh = 0;
  pTimer->Control = 0x00000003;
  return  pTimer->CurrentValueLow;
}

/*
 * Get timer value
 */
static int get_timer()
{
  PStruct_TIMER pTimer = (PStruct_TIMER)(TIMERX6_BASE);
  return  pTimer->CurrentValueLow;
}


/*
 * Stop timer
 */
static int stop_timer()
{
  int temp;
  PStruct_TIMER pTimer = (PStruct_TIMER)(TIMERX6_BASE);
  temp = pTimer->CurrentValueLow;
  pTimer->Control = 0x00000002;
  return temp;
}

#ifdef __cplusplus
}
#endif
#endif

